from device_list import devicelist
from get_status_xml import *
from qyt_netconf_connect import netconf_connect
import xml.etree.ElementTree as ET
import sys
sys.path.append('/home/qyt/PycharmProjects/pythonProject/venv/My_Code/My_modules')
print('='*80)
print(sys.path)
from ip10_to_ipdot import *

def get_status_data_xml(host_ip, params):
    m = netconf_connect(host_ip, params)
    status = m.get(get_status).data_xml   # 将返回数据存储为XML文本 #
    return status

def find_text(tag):
    tree = ET.fromstring(get_status_data_xml(host_ip, params))   # 将文本以etree形式实例化到变量tree #
    # print(type(tree))
    # for i in tree.iter():  # 使用for循环遍历etree的所有标签 #
    #     print(i.tag)
    tag_text=tree.findtext(".//" + tag)
    return tag_text

def if_dot(RID):
    if RID.find('.') == -1:  #如果在返回结果中查找不到“.”则将其转换为点分十进制
        PEER_RID=ip10_to_ip(int(RID)) #调用自建代码库中的代码块来完成IP格式转换操作
    else:
        PEER_RID=RID
    return PEER_RID

for i in range(len(devicelist)):
    device_name = devicelist[i]['name']
    host_ip = devicelist[i]['ip']
    params = devicelist[i]['params']

    if params == 'csr':
        get_status = get_status1
        peer_rid_tag = '{http://cisco.com/ns/yang/Cisco-IOS-XE-ospf-oper}nbr-id'
    elif params == 'huawei':
        get_status = get_status0
        peer_rid_tag = '{http://www.huawei.com/netconf/vrp}nbrRouterId'

    PEER_RID=find_text(peer_rid_tag)
    final_rid=if_dot(PEER_RID)
    print('=' * 80)
    print("{}'s ospf neighbor router-id is {}".format(device_name,final_rid))

'''
import ipaddress
ipaddress.ip_address(3221225985)
'''

